<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="场馆id" prop="venueId" v-if="false">
        <el-input
          v-model="queryParams.venueId"
          placeholder="请输入场馆id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="场馆" prop="venueId">
            <el-select v-model="queryParams.venueId" placeholder="请选择场馆" >
              <el-option
                  v-for="item in allVenueOptions"
                  :key="item.venueId"
                  :label="item.venueName"
                  :value="item.venueId"
              ></el-option>
            </el-select> 
      </el-form-item>

      <el-form-item label="用户id" prop="userId" v-if="false">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="预订日期" prop="bookingDate">
        <el-date-picker clearable
          v-model="queryParams.bookingDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预订日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预定时段" prop="bookingTime" v-if="false">
        <el-input
          v-model="queryParams.bookingTime"
          placeholder="请输入预定时段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hj:bookings:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hj:bookings:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hj:bookings:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hj:bookings:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookingsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="bookingId" v-if="false"/>
      <el-table-column label="场馆id" align="center" prop="venueId" v-if="false"/>

      <el-table-column label="场馆名称" align="center" prop="venueName" />

      <el-table-column label="用户id" align="center" prop="userId" v-if="false"/>

      <el-table-column label="预约人" align="center" prop="bookingBy" />
      <el-table-column label="预约人账户" align="center" prop="bookingAccount" />
      <el-table-column label="预约人电话" align="center" prop="phonenumber" />

      <el-table-column label="预约日期" align="center" prop="bookingDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bookingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约时段" align="center" prop="bookingTime" v-if="false"/>
      <el-table-column prop="bookingTime" label="预约时段">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_booking_time" :value="scope.row.bookingTime"/>
        </template>
      </el-table-column>

      <el-table-column label="预约状态" align="center" prop="bookingStatus" v-if="false"/>
      <el-table-column prop="bookingStatus" label="预约状态">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_booking_status" :value="scope.row.bookingStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hj:bookings:edit']"
            v-show="scope.row.bookingStatus==0"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hj:bookings:edit']"
            v-show="scope.row.bookingStatus==2"
          >打回修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hj:bookings:remove']"
            v-show="scope.row.bookingStatus==0"
          >删除</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleBookingSubmit(scope.row.bookingId)"
            v-hasPermi="['hj:bookings:bookingSubmit']"
            v-show="scope.row.bookingStatus==0|scope.row.bookingStatus==2"
          >提交预约</el-button>

        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预订对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="场馆" prop="venueId">
             <el-select v-model="form.venueId" placeholder="请选择场馆">
                <el-option
                  v-for="item in venueOptions"
                  :key="item.venueId"
                  :label="item.venueName"
                  :value="item.venueId"
                ></el-option>
              </el-select> 
        </el-form-item>


        <el-form-item label="预订日期" prop="bookingDate">
          <el-date-picker clearable
            v-model="form.bookingDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预订日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="预定时段" prop="bookingTime" v-if="false">
          <el-input v-model="form.bookingTime" placeholder="请输入预定时段" />
        </el-form-item>
        <el-form-item label="预定时段" prop="bookingTime">
              <el-radio-group v-model="form.bookingTime">
                <el-radio
                  v-for="dict in dict.type.sys_booking_time"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
        </el-form-item>

        <el-tabs v-if="result">  
              
          <el-tab-pane label="审批结果">  
            <el-table :data="tableData" style="width: 100%">  
              <el-table-column prop="id" label="编号">  
                <template slot-scope="scope">  
                  {{ generateUniqueId(scope.$index) }}  
                </template> 
              </el-table-column>  
              <el-table-column prop="userName" label="任务办理人">  
              </el-table-column>          
              <el-table-column prop="taskKeyId" label="任务的节点">  
              </el-table-column>  
               <el-table-column prop="taskName" label="任务的名称">  
              </el-table-column> 
              <el-table-column prop="message" label="批注">  
              </el-table-column>  
            </el-table>  
          </el-tab-pane>  
      
        </el-tabs>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBookings, getBookings, delBookings, addBookings, updateBookings, submitBooking, getBookingComment } from "@/api/hj/bookings";
import { getVenuesBystatus, getAllVenues } from "@/api/hj/venues";

export default {
  name: "Bookings",
  dicts: ['sys_booking_status', 'sys_booking_time'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预订表格数据
      bookingsList: [],

      //所有可预约场馆信息
      venueOptions: [],
      //所有场馆信息
      allVenueOptions: [],

      tableData: [],
      //是否显示审批结果
      result: false,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        venueId: null,
        userId: null,
        bookingDate: null,
        bookingTime: null,
        bookingStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        venueId: [
          { required: true, message: "场馆id不能为空", trigger: "blur" }
        ],
        bookingDate: [
          { required: true, message: "预订日期不能为空", trigger: "blur" }
        ],
        bookingTime: [
          { required: true, message: "预定时段不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();

    getVenuesBystatus().then(response => {
        this.venueOptions = response.data;
      });
      
  },
  methods: {
    /** 查询预订列表 */
    getList() {
      this.loading = true;
      listBookings(this.queryParams).then(response => {
        console.log(response);
        this.bookingsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

      getAllVenues().then(response => {
        this.allVenueOptions = response.data;
      });

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bookingId: null,
        venueId: null,
        userId: null,
        bookingDate: null,
        bookingTime: null,
        bookingStatus: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
      this.result= false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bookingId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预订";
      
      
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bookingId = row.bookingId || this.ids
      getBookingComment(bookingId).then(response => {
        console.log(response);
        this.form = response.data;
        this.open = true;
        this.result = true;
        this.title = "修改预订";
        this.tableData=response.data.taskCommentVoList

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bookingId != null) {
            updateBookings(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBookings(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bookingIds = row.bookingId || this.ids;
      this.$modal.confirm('是否确认删除预订编号为"' + bookingIds + '"的数据项？').then(function() {
        return delBookings(bookingIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hj/bookings/export', {
        ...this.queryParams
      }, `bookings_${new Date().getTime()}.xlsx`)
    },

    handleBookingSubmit(bookingId){
        submitBooking(bookingId).then(response => {
          console.log(response)
          this.getList();
          this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + "提交申请成功！" + "</div>", "提示框", { dangerouslyUseHTMLString: true });
       });
   },

   generateUniqueId(index) {
      // 根据需要生成唯一的编号
      return '' + (index + 1);
    }

  }
};
</script>
